package com.lyp.lease.common.exception;

import com.lyp.lease.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * ExceptionHandler注解捕获全部异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<String> error (Exception e) {
        System.out.println("GlobalExceptionHandler执行了----------------->");
        e.printStackTrace();
        return Result.fail(e.getMessage());
    }

    /**
     * ExceptionHandler注解只捕获抛出LeaseException的Controller的异常
     * @param e
     * @return
     */
    @ExceptionHandler(LeaseException.class)
    @ResponseBody
    public Result error(LeaseException e){
        e.printStackTrace();
        return Result.fail(e.getCode(), e.getMessage());
    }
}
